// pages/hydz/index.js
var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
var qqmapsdk;
const chooseLocation = requirePlugin('chooseLocation');//导入插件
Page({

  /**
   * 页面的初始数据
   */
  data: {
    latitude: 39.89631551,
    longitude: 116.323459711,
    currentDw:"",
    markers:[{
      id: 0,
      name:"no",
      iconPath: "../../images/dwdw.png",
      latitude:  39.89631551,
      longitude:  116.323459711,
      width: 10,
      height: 12,
      title:'sss'
    }],
    searhcon:'',
    qylist:[],
    qydata:[
      {
        qyname:'四会市东城区,XXXXXXX大大大',
        qyincro:'XXX,四会市东城区,XXXXXXX四会市东城区,XXXXXXX四会市东城区,XXXXXXX四会市东城区,XXXXXXX四会市东城区,XXXXXXX',
        zb:{
          latitude:  39.920026,
          longitude: 116.339303,
        }
      },
      {
        qyname:'四会市东城区,XX1XXXXX',
        qyincro:'XXX,四会市东城区,XXXXXXX四会市东城区,XXXXXXX四会市东城区,XXXXXXX四会市东城区,XXXXXXX四会市东城区,XXXXXX1X',
        zb:{
          latitude:  40.001765,
          longitude: 116.310126,
        }
      },
      {
        qyname:'四会市东城区,XXX2XXXX',
        qyincro:'XXX,四会市东城区,XXXXXXX四会市东城区,XXXXXXX四会市东城区,XXXXXXX四会市东城区,XXXXXXX四会市东城区,X2XXXXXX',
        zb:{
          latitude: 39.304802,
          longitude: 118.004834,
        }
      },
      {
        qyname:'四会市东城区,X3XXXXXX',
        qyincro:'XXX,四会市东城区,XXXXXXX四会市东城区,XXXXXXX四会市东城区,XXXXXXX四会市东城区,XXXXXXX四会市东城区,XX3XXXXX',
        zb:{
          latitude:  39.920026,
          longitude: 116.339303,
        }
      },
      {
        qyname:'四会市东城区,XXXX4XXX',
        qyincro:'XXX,四会市东城区,XXXXXXX四会市东城区,XXXXXXX四会市东城区,XXXXXXX四会市东城区,XXXXXXX四会市东城区,XXX4XXXX',
        zb:{
          latitude:  39.920026,
          longitude: 116.339303,
        }
      },
      {
        qyname:'四会市东城区,XXX5XXXX',
        qyincro:'XXX,四会市东城区,XXXXXXX四会市东城区,XXXXXXX四会市东城区,XXXXXXX四会市东城区,XXXXXXX四会市东城区,X5XXXXXX',
        zb:{
          latitude:  39.920026,
          longitude: 116.339303,
        }
      }
    ]
  },
  initMap(){
     // 实例化API核心类
    qqmapsdk = new QQMapWX({
         key: 'AQYBZ-6SLCP-VH2DI-VTGDZ-CYAA6-M3B3E'
     });
  },
  openDh(){
    let that =this;
    // success
    wx.openLocation({
     latitude: that.data.latitude, 
     longitude: that.data.longitude, 
     scale: 28, // 缩放比例
    })
  },
  getAdress(e){
    console.log(e)
    console.log(e.currentTarget.dataset.sdzd.zb)
    let sdzd = e.currentTarget.dataset.sdzd;
    this.setData({
      latitude: sdzd.zb.latitude,
      longitude: sdzd.zb.longitude,
      ['markers[0].latitude']:sdzd.zb.latitude,
      ['markers[0].longitude']:sdzd.zb.longitude,
      currentDw:sdzd.qyname
    })
    console.log(this.data.markers)
  },
  search(){
      let sear=this.data.searhcon;
      let searchData= this.data.qydata.filter(item=>{
        return item.qyname.indexOf(sear)!==-1;
      })
      console.log(searchData)
      this.setData({
        qylist:searchData
      })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this= this;
     this.initMap();
     this.setData({
       qylist: _this.data.qydata,
       currentDw:_this.data.qydata[0].qyname
     })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const location = chooseLocation.getLocation(); // 如果点击确认选点按钮，则返回选点结果对象，否则返回null
    console.log(location)  //得到你刚刚选择的地址信息
    if(location){
      var markersLat ="markers["+0+"].latitude"
      var markersLon ="markers["+0+"].longitude"
      this.setData({
        latitude:  location.latitude,
        longitude: location.longitude,
        markers:[{
          id: 0,
          name:"no",
          iconPath: "../../images/dh.png",
          latitude:  location.latitude,
          longitude: location.longitude,
          width: 10,
          height: 10,
          title:'sss'
        }]
      })
      this.initMap()
    }
 

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})